package com.liuc.practice.bank.transfer.service.impl;

import com.liuc.practice.bank.transfer.annotation.Autowired;
import com.liuc.practice.bank.transfer.annotation.Service;
import com.liuc.practice.bank.transfer.annotation.Transactional;
import com.liuc.practice.bank.transfer.dao.AccountDao;
import com.liuc.practice.bank.transfer.pojo.Account;
import com.liuc.practice.bank.transfer.service.TransferService;
import com.liuc.practice.bank.transfer.utils.TransactionManager;

/**
 * @Author Liu Chang
 * @Date 2021/2/24 9:18 下午
 */
@Service("transferService")
@Transactional
public class TransferServiceImpl implements TransferService {

    @Autowired
    private AccountDao accountDao;

    public void setAccountDao (AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public AccountDao getAccountDao() {
        return accountDao;
    }

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney()-money);
        to.setMoney(to.getMoney()+money);

        // 这里两次调用使用了两个不同的 dao
        accountDao.updateAccountByCardNo(to);

         int i = 1/0;
        accountDao.updateAccountByCardNo(from);

    }
}
